package com.leo;

import com.almasb.fxgl.app.CursorInfo;
import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.core.math.FXGLMath;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.SpawnData;
import com.leo.common.Config;
import com.leo.component.TankComponent;
import com.leo.entity.PlayerEntityFactory;
import javafx.geometry.Rectangle2D;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;

import java.util.Map;

public class TankApp extends GameApplication {

    private TankComponent playerComponent;

    @Override
    protected void initSettings(GameSettings settings) {
        settings.setTitle("Tank");
        settings.setVersion("0.1");
        settings.setWidth(28 * Config.CELL_SIZE + 6 * Config.CELL_SIZE);
        settings.setHeight(28 * Config.CELL_SIZE);
        settings.setAppIcon("ui/icon.png");
        settings.setDefaultCursor(new CursorInfo("ui/cursor.png", 0, 0));
        settings.setDeveloperMenuEnabled(true);
        settings.setProfilingEnabled(true);
    }

    @Override
    protected void initGameVars(Map<String, Object> vars) {

    }

    @Override
    protected void initInput() {
        FXGL.onKey(KeyCode.UP, () -> {
            playerComponent.moveUp();
        });
        FXGL.onKey(KeyCode.DOWN, () -> {
            playerComponent.moveDown();
        });
        FXGL.onKey(KeyCode.LEFT, () -> {
            playerComponent.moveLeft();
        });
        FXGL.onKey(KeyCode.RIGHT, () -> {
            playerComponent.moveRight();
        });
        FXGL.onKey(KeyCode.SPACE, () -> {
            playerComponent.shoot();
        });
    }

    @Override
    protected void initGame() {
        // 1.设置背景色为黑色
        FXGL.getGameScene().setBackgroundColor(Color.BLACK);

        // 2.创建游戏实体工厂类
        FXGL.getGameWorld().addEntityFactory(new PlayerEntityFactory());

        // 3.创建游戏实体并添加到游戏世界中
        FXGL.spawn("brick", new SpawnData(160, 50));
        FXGL.spawn("greens", FXGLMath.randomPoint(new Rectangle2D(0, 0, FXGL.getAppWidth(), FXGL.getAppHeight())));
        FXGL.spawn("snow", new SpawnData(FXGLMath.random(0, FXGL.getAppWidth()), FXGLMath.random(0, FXGL.getAppHeight())));
        FXGL.spawn("stone", new SpawnData(FXGLMath.random(0, FXGL.getAppWidth()), FXGLMath.random(0, FXGL.getAppHeight())));
        FXGL.spawn("sea", new SpawnData(FXGLMath.random(0, FXGL.getAppWidth()), FXGLMath.random(0, FXGL.getAppHeight())));

        Entity player = FXGL.spawn("player", 100, 100);
        playerComponent = player.getComponent(TankComponent.class);

    }

    @Override
    protected void initUI() {

    }

    public static void main(String[] args) {
        launch(args);
    }
}
